<?php
/**
 * Created by PhpStorm.
 * User: Wodro
 * Date: 2019/9/28
 * Time: 12:04
 */

namespace api\controllers;

use common\data\Enum;
use common\models\db\AppVersion;
use common\models\db\Article;
use common\models\db\LogEmailSend;
use common\models\db\SearchIndex;
use Mimey\MimeTypes;
use wodrow\yii\rest\Controller;

class PublicController extends Controller
{
    /**
     * 获取api基础链接地址
     * @desc post|get onlyDataOut
     * @return array
     * @return string apiUrl
     */
    public function actionGetApiUrl()
    {
        $this->onlyDataOut = true;
        return [
            "apiUrl" => \Yii::$app->uniApp->apiUrl,
        ];
    }

    /**
     * 获取枚举信息
     * @desc post
     * @return array
     * @return object enums
     * @return object enums.logEmailSend
     * @return array enums.logEmailSend.TypeDesc 验证码类型
     * @return object enums.appVersion APP
     * @return array enums.appVersion.TypeDesc 版本app类型
     * @return array enums.article 文章
     * @return array enums.article.statusDesc 状态类型
     * @return array enums.article.isBoutiqueDesc 是否精品
     * @return array enums.searchIndex 搜索索引
     * @return array enums.searchIndex.typeDesc 类型
     */
    public function actionGetEnums()
    {
        $enums = [
            'logEmailSend' => [
                'typeDesc' => Enum::getEmailSendTypeDesc(),
            ],
            'appVersion' => [
                'typeDesc' => AppVersion::instance()->typeDesc,
            ],
            'article' => [
                'statusDesc' => Article::instance()->statusDesc,
                'isBoutiqueDesc' => Article::instance()->isBoutiqueDesc,
                'createTypeDesc' => Article::instance()->createTypeDesc,
            ],
            'searchIndex' => [
                'typeDesc' => SearchIndex::instance()->typeDesc,
            ],
        ];
        return $this->success("获取成功", ['enums' => $enums]);
    }

    /**
     * 获取后台服务信息
     * @desc post
     * @return array
     * @return object datas
     * @return string datas.adminEmail
     * @return string datas.adminQQ
     * @return string datas.adminWX
     * @return string datas.apiAdminUserIds
     * @return array datas.sysInfo
     * @return array datas.howToUse
     * @return array datas.warnings
     * @return array datas.qqqs
     * @return string datas.searchAllKeyword
     */
    public function actionGetDatas()
    {
        $params = \Yii::$app->params;
        $datas = [];
        $datas['adminEmail'] = $params['adminEmail'];
        $datas['adminQQ'] = $params['adminQQ'];
        $datas['adminWX'] = $params['adminWX'];
        $datas['apiAdminUserIds'] = $params['apiAdminUserIds'];
        $datas['sysInfo'] = \Yii::$app->uniApp->sysInfo;
        $datas['howToUse'] = \Yii::$app->uniApp->howToUse;
        $datas['warnings'] = \Yii::$app->uniApp->warnings;
        $datas['qqqs'] = \Yii::$app->uniApp->qqqs;
        $datas['searchAllKeyword'] = \Yii::$app->uniApp->searchAllKeyword;
        return $this->success("正常", [
            'datas' => $datas,
        ]);
    }

    /**
     * 显示随机头像
     * @desc get 显示image
     */
    public function actionRandomAvatar()
    {
        $avatarUrl = \Yii::$app->apiTool->randomAvatarUrl;
        $mimes = new MimeTypes();
        $mineType = $mimes->getMimeType('jpg');
        header("content-type:{$mineType};");
        $content=file_get_contents($avatarUrl);
        echo $content;
    }
}